﻿
﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using NyBio.Model;

namespace NyBio.DB
{
    public class SeatDB
    {
        private Connection connection;
        private RoomDB roomDB;

        public SeatDB()
        {
            connection = new Connection();
            roomDB = new RoomDB();
        }

        public int getColCount(int roomId)
        {
            string query = "SELECT MAX(cul) FROM XpSeat WHERE xpRoom_id =" +
                roomId;
            connection.Open();
            SqlDataReader reader = connection.Read(query);

            if (reader.Read())
                return reader.GetInt32(0);
            else
                return -1;
        }

        public int getRowCount(int roomId)
        {
            string query = "SELECT MAX(row) FROM XpSeat WHERE xpRoom_id =" +
                roomId;
            connection.Open();
            SqlDataReader reader = connection.Read(query);

            if (reader.Read())
                return reader.GetInt32(0);
            else
                return -1;
        }

        public List<Seat> getSeats()
        {
            return where("");
        }

        public Seat getSeatById(int id)
        {
            return singleWhere("xpSeat_id = " + id);
        }

        public List<Seat> getSeatsByRoom(int roomId)
        {
            return where("xpRoom_id = " + roomId);
        }

        private Seat singleWhere(String wClause)
        {
            Seat seat = null;

            string query = "SELECT * FROM XpSeat WHERE " + wClause;
            connection.Open();
            SqlDataReader reader = connection.Read(query);
            if (reader.Read())
            {
                seat = new Seat();
                seat._id = reader.GetInt32(0);
                seat._roomId = reader.GetInt32(1);
                seat._row = reader.GetInt32(2);
                seat._col = reader.GetInt32(3);
            }

            reader.Close();
            connection.Close();

            return seat;
        }

        private List<Seat> where(String wClause)
        {
            List<Seat> seats = new List<Seat>();

            string query;
            if (!wClause.Equals(""))
                query = "SELECT * FROM XpSeat WHERE " + wClause;
            else
                query = "SELECT * FROM XpSeat";
            connection.Open();
            SqlDataReader reader = connection.Read(query);

            while (reader.Read())
            {
                Seat seat = new Seat();
                seat._id = reader.GetInt32(0);
                seat._row = reader.GetInt32(1);
                seat._col = reader.GetInt32(2);
                seat._reserved = reader.GetBoolean(3);
                seat._roomId = reader.GetInt32(4);
                seats.Add(seat);
            }

            reader.Close();
            connection.Close();

            return seats;
        }

        public void removeSeat(int id)
        {
            string query = "DELETE FROM XpSeat WHERE xpSeat_id=" + id;
            connection.Open();
            connection.Execute(query);
            connection.Close();

        }

        public void insertSeat(Seat seat)
        {
            string query = @"INSERT INTO XpSeat (roomId, row, cul, reserved) " +
                "VALUES ( " + seat._roomId
                + ", " + seat._row 
                + ", " + seat._col 
                + ", " + seat._reserved + ")";
            connection.Open();
            connection.Execute(query);
            connection.Close();
        }

        public void updateSeat(Seat seat)
        {
            string query = @"UPDATE Seats SET xpRoom_id =" + seat._roomId + ", cul=" + seat._row + ", cul=" + seat._col + ", cul=" + seat._reserved +
                " WHERE xpSeat_id=" + seat._id;
            connection.Open();
            connection.Execute(query);
            connection.Close();

        }

    }
}